package com.kaka.bus.engine.home.info.bank;


import android.content.Intent;
import android.graphics.Color;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.adapter.BankManagerAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.BankManagerBean;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.home.info.bank.add.AddBankActivity;
import com.kaka.bus.listener.Listener;
import com.kaka.bus.util.U;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by zhangqi on 2017/4/27.
 */

public class BankCardManagerActivity extends BaseActivity<BankCardManagerPresenter> implements BankCardManagerContract.View {


    @BindView(R.id.toolbar_step_back)
    ImageView mToolbarStepBack;
    @BindView(R.id.toolbar_step_name)
    TextView mToolbarStepName;
    @BindView(R.id.toolbar_step_right)
    TextView mToolbarStepRight;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.bank_manager_tips)
    TextView mBankManagerTips;
    @BindView(R.id.emptyview)
    FrameLayout mEmptyview;
    @BindView(R.id.bank_manager_recy)
    RecyclerView mBankManagerRecy;
    @BindView(R.id.addbank_swipe)
    SwipeRefreshLayout mAddbankSwipe;

    private List<BankManagerBean> mList = new ArrayList<>();
    private BankManagerAdapter mBankManagerAdapter;

    private String mChangeBankStatusTips;

    @Override
    protected void initView() {
        super.initView();
        mToolbarStepBack.setOnClickListener(v -> finish());
        mToolbarStepName.setText("银行卡管理");
        mToolbarStepRight.setText(R.string.add);

        mAddbankSwipe.setProgressViewEndTarget(false,300);

        SpannableStringBuilder spannableStringBuilder = U.toRichText(getString(R.string.bank_manager_tips), getResources().getColor(R.color.red_money), 0, 1);
        mBankManagerTips.setText(spannableStringBuilder);

        mBankManagerRecy.setLayoutManager(new LinearLayoutManager(this));
        mBankManagerAdapter = new BankManagerAdapter(this, mList);
        mBankManagerRecy.setAdapter(mBankManagerAdapter);

        mAddbankSwipe.setColorSchemeColors(Color.parseColor(Constant.TOOLBAR_COLOR));
        mAddbankSwipe.setRefreshing(true);
    }

    @Override
    protected void initData() {
        mPresenter.getBankCardManagerList(getUserId());
    }

    @Override
    protected void initListener() {
        mToolbarStepRight.setOnClickListener(v -> {
            mPresenter.canBinding(getUserId());
        });

        mBankManagerAdapter.setListener((position, data) -> {
            if ("1".equals(data.getStatus())) {
                showMessage("此卡已经是主卡");
            } else {
                U.getSlectDialog(this, "温馨提示", "是否设置该卡为主卡", "取消", "确定",
                        new Listener.OnNormalAlertDialogChooseClickListener() {
                            @Override
                            public void onLeft() {

                            }

                            @Override
                            public void onRight() {
                                mPresenter.changeBankStatus(getUserId(), data.getBankNum());
                            }
                        }).show();
            }
        });

        mAddbankSwipe.setOnRefreshListener(this::initData);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bankmanager;
    }


    @Override
    public void getBankCardManagerListSuccrss(HttpWrapper<List<BankManagerBean>> wrapper) {
        closeRefresh();
        if(Constant.SUCCESS_CODE.equals(wrapper.code)){
            List<BankManagerBean> data = wrapper.getData();

            mList.clear();

            if (data == null || data.size() <= 0) {
                mEmptyview.setVisibility(View.VISIBLE);
                return;
            }else{
                mList.addAll(data);
                mBankManagerAdapter.notifyDataSetChanged();
                mEmptyview.setVisibility(View.GONE);
            }

            if (!TextUtils.isEmpty(mChangeBankStatusTips)) {
                U.getSingleDialog(this,"温馨提示",mChangeBankStatusTips,"确定",null).show();
                mChangeBankStatusTips = null;
            }
        }else {
            showMessage(wrapper.getInfo());
        }

    }

    @Override
    public void changeBankStatusSuccess(HttpWrapper<String> wrapper) {
        if(Constant.SUCCESS_CODE.equals(wrapper.code)){
            String data = wrapper.data;

            mChangeBankStatusTips = data;
            initData();
        }else{
            showMessage(wrapper.getInfo());
        }

    }

    public void closeRefresh(){
        if(mAddbankSwipe == null){
            return;
        }

        if(mAddbankSwipe.isRefreshing()){
            mAddbankSwipe.setRefreshing(false);
        }
    }

    @Override
    public void can(HttpWrapper<String> wrapper) {
        if(Constant.SUCCESS_CODE.equals(wrapper.code)){
            startActivityForResult(new Intent(this, AddBankActivity.class),1);
        }else{
            showMessage(wrapper.getInfo());
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initData();
    }


    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }
}
